// pages/chartInfo/chartInfo.js
var wxCharts = require('../../utils/wxcharts-min.js');
var app = getApp();
var lineChart = null;
Page({
  data: {
    listMedicalCategoryDate: {},
    comb_name:'',
    check_date: ''
  },
  touchHandler: function (e) {
    console.log(lineChart.getCurrentDataIndex(e));
    lineChart.showToolTip(e, {
      // background: '#7cb5ec',
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  }, 
  onLoad: function (e) {
    var windowWidth = 320;
    var that = this;
    var combName = e.combName;
    var checkDate = e.checkDate;
    var userInfo = wx.getStorageSync('trUserInfo');
    var listCheckDate = [];
    var listMedicalCategory = [];
    this.setData({
      comb_name: combName,
      check_date: checkDate
    })
    wx.setNavigationBarTitle({
      title: "我的" + combName + "分析"//页面标题为路由参数
    })
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    wx.request({
      url: getApp().globalData.host_url + '/medicalExReport/ajax_showBloodReportByUser?userId=' + userInfo.userId + '&combName=' + combName + '&checkDate=' + checkDate,
      method: 'GET',
      data: {},
      success: function (res) {
        listCheckDate = res.data.listCheckDate;
        listMedicalCategory = res.data.medicalData;
        if (listCheckDate == null) {
          var textToast = "信息获取失败！";
          wx.showToast({
            title: textToast,
            icon: '',
            duration: 2000
          })
        } else {
          var medicalData;
          that.setData({
            listMedicalCategoryDate: res.data.listMedicalCategoryDate
          })
          lineChart = new wxCharts({
            canvasId: 'lineCanvas',
            type: 'line',
            categories: listCheckDate,
            series: listMedicalCategory,
            yAxis: {
              title: false,
              format: function (val) {
                return val.toFixed(2);
              },
              min: 0
            },
            height: 150,
            width: windowWidth,
            legend: false,
            dataLabel: false
          });
        }
      }
    })
  }
});